<?php

namespace Kanin\Games\model;

use Kanin\Games\lib\Tool;

class TableIds extends BaseModel {

	const TABLE_NAME = 'games.table_ids';

	public function makeId($table) {
		$find = Tool::mongo('main')->collection(self::TABLE_NAME)->filter(['_id' => $table])->getOne();

		if (!$find) {
			$data = [
				'_id' => $table,
				'table_name' => $table,
				'id' => 1
			];
			$res = Tool::mongo('main')->collection(self::TABLE_NAME)->insert($data);
			if (!$res) {
				return false;
			}
			$id = 1;
		} else {
			$data = [
				'$inc' => ['id'=>1]
			];
			$res = Tool::mongo('main')->collection(self::TABLE_NAME)->filter(['_id'=> $table])->update($data, ['upsert'=>true]);
			if (!$res) {
				return false;
			}
			$id = $find->id + 1;
		}

		return $id;
	}
}